package com.lewis.generator.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lewis.core.constant.GenConstants;
import com.lewis.core.base.domain.BaseEntity;
import com.lewis.core.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ArrayUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * 业务表 gen_table
 *
 * @author Lewis
 */
@EqualsAndHashCode(callSuper = true)
@ApiModel("业务表 gen_table")
@Data
public class GenTable extends BaseEntity {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "编号")
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;

	@ApiModelProperty(value = "表名称")
	@NotBlank(message = "表名称不能为空")
	private String tableName;

	@ApiModelProperty(value = "表描述")
	@NotBlank(message = "表描述不能为空")
	private String tableComment;

	@ApiModelProperty(value = "关联父表的表名")
	private String subTableName;

	@ApiModelProperty(value = "本表关联父表的外键名")
	private String subTableFkName;

	@ApiModelProperty(value = "实体类名称(首字母大写)")
	@NotBlank(message = "实体类名称不能为空")
	private String className;

	@ApiModelProperty(value = "使用的模板（crud单表操作 tree树表操作 sub主子表操作）")
	private String tplCategory;

	@ApiModelProperty(value = "生成包路径")
	@NotBlank(message = "生成包路径不能为空")
	private String packageName;

	@ApiModelProperty(value = "生成模块名")
	@NotBlank(message = "生成模块名不能为空")
	private String moduleName;

	@ApiModelProperty(value = "生成业务名")
	@NotBlank(message = "生成业务名不能为空")
	private String businessName;

	@ApiModelProperty(value = "生成功能名")
	@NotBlank(message = "生成功能名不能为空")
	private String functionName;

	@ApiModelProperty(value = "生成作者")
	@NotBlank(message = "作者不能为空")
	private String functionAuthor;

	@ApiModelProperty(value = "生成代码方式（0zip压缩包 1自定义路径）")
	private String genType;

	@ApiModelProperty(value = "生成路径（不填默认项目路径）")
	private String genPath;

	@ApiModelProperty(value = "主键信息")
	@TableField(exist = false)
	private GenTableColumn pkColumn;

	@ApiModelProperty(value = "子表信息")
	@TableField(exist = false)
	private GenTable subTable;

	@ApiModelProperty(value = "表列信息")
	@Valid
	@TableField(exist = false)
	private List<GenTableColumn> columns;

	@ApiModelProperty(value = "其它生成选项")
	private String options;

	@ApiModelProperty(value = "树编码字段")
	@TableField(exist = false)
	private String treeCode;

	@ApiModelProperty(value = "树父编码字段")
	@TableField(exist = false)
	private String treeParentCode;

	@ApiModelProperty(value = "树名称字段")
	@TableField(exist = false)
	private String treeName;

	@ApiModelProperty(value = "上级菜单ID字段")
	@TableField(exist = false)
	private String parentId;

	@ApiModelProperty(value = "上级菜单名称字段")
	@TableField(exist = false)
	private String parentMenuName;

	@ApiModelProperty(value = "创建者")
	private String createBy;

	@ApiModelProperty(value = "创建时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	@ApiModelProperty(value = "更新者")
	private String updateBy;

	@ApiModelProperty(value = "更新时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date updateTime;

	@ApiModelProperty(value = "备注")
	private String remark;

	public static boolean isSub(String tplCategory) {
		return tplCategory != null && StringUtil.equals(GenConstants.TPL_SUB, tplCategory);
	}

	public static boolean isTree(String tplCategory) {
		return tplCategory != null && StringUtil.equals(GenConstants.TPL_TREE, tplCategory);
	}

	public static boolean isCrud(String tplCategory) {
		return tplCategory != null && StringUtil.equals(GenConstants.TPL_CRUD, tplCategory);
	}

	public static boolean isSuperColumn(String tplCategory, String javaField) {
		if (isTree(tplCategory)) {
			return StringUtil.equalsAnyIgnoreCase(javaField,
					ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
		}
		return StringUtil.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
	}

	public boolean isSub() {
		return isSub(this.tplCategory);
	}

	public boolean isTree() {
		return isTree(this.tplCategory);
	}

	public boolean isCrud() {
		return isCrud(this.tplCategory);
	}

	public boolean isSuperColumn(String javaField) {
		return isSuperColumn(this.tplCategory, javaField);
	}
}